package com.oswego.dawcs;

import java.util.ArrayList;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.res.TypedArray;
import android.os.Parcel;
import android.os.Parcelable;
import android.preference.DialogPreference;
import android.text.Editable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

public class CustomListPreference extends DialogPreference {

	private static final Integer DEFAULT_VALUE = 0;

	private ArrayList<String> entries = null;
	private ArrayList<Integer> entryValues = null;
	private Integer selected = null;

	public CustomListPreference(Context context, AttributeSet attrs) {
		super(context, attrs);

		entries = new ArrayList<String>();
		entryValues = new ArrayList<Integer>();

		setDialogLayoutResource(R.layout.pref_list_dialog);

		setNegativeButtonText("Cancel");
		setPositiveButtonText("Save");
	}

	public void setEntries(ArrayList<String> entries) {
		this.entries = entries;
	}

	public void setEntryValues(ArrayList<Integer> entryValues) {
		this.entryValues = entryValues;
	}

	public void addEntry(String entry, Integer value) {
		entries.add(entry);
		entryValues.add(value);
	}

	private void setSelected(String s) {
		selected = getValue(s);
	}

	private void setSelected(Integer i) {
		selected = i;
		persistInt(selected);
	}

	private String getEntry(Integer i) {
		int loc = entryValues.indexOf(i);
		return entries.get(loc);
	}

	private Integer getValue(String s) {
		int loc = entries.indexOf(s);
		return entryValues.get(loc);
	}

	@Override
	protected void onSetInitialValue(boolean restore, Object defaultValue) {
		setSelected(restore ? getPersistedInt(DEFAULT_VALUE)
				: (Integer) defaultValue);
	}

	@Override
	protected Object onGetDefaultValue(TypedArray a, int index) {
		return a.getString(index);
	}

	@Override
	protected void onBindDialogView(View view) {
		super.onBindDialogView(view);

		TextView dialogMessageText = (TextView) view
				.findViewById(R.id.text_dialog_message);
		dialogMessageText.setText(getDialogMessage());

		// set up listview
		ListView listview = (ListView) view.findViewById(R.id.listview);
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(
				this.getContext(), android.R.layout.simple_list_item_1, entries);
		listview.setAdapter(adapter);

		listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				setSelected((String) parent.getItemAtPosition(position));
			}
		});
	}

	@Override
	protected void onDialogClosed(boolean positiveResult) {
		super.onDialogClosed(positiveResult);

		if (positiveResult) {
            persistInt(selected);
			if (selected == -1) {
				AlertDialog.Builder alert = new AlertDialog.Builder(
						this.getContext());

				alert.setTitle("Enter New Template Name");

				final EditText input = new EditText(this.getContext());
				alert.setView(input);

				alert.setPositiveButton("Save",
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog,
									int whichButton) {
								Editable value = input.getText();
								// TODO use the inputed value
							}
						});

				alert.setNegativeButton("Cancel",
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog,
									int whichButton) {
							}
						});

				alert.show();
			}
		}
	}

	@Override
	protected Parcelable onSaveInstanceState() {
		final Parcelable superState = super.onSaveInstanceState();

		final SavedState myState = new SavedState(superState);
		myState.entries = entries;
		myState.entryValues = entryValues;
		myState.selected = selected;

		return myState;
	}

	@Override
	protected void onRestoreInstanceState(Parcelable state) {
		if (state == null || !state.getClass().equals(SavedState.class)) {
			super.onRestoreInstanceState(state);
			return;
		}
		SavedState myState = (SavedState) state;
		entries = myState.entries;
		entryValues = myState.entryValues;
		setSelected(myState.selected);

		super.onRestoreInstanceState(myState.getSuperState());
	}

	private static class SavedState extends BaseSavedState {
		ArrayList<String> entries;
		ArrayList<Integer> entryValues;
		Integer selected;

		public SavedState(Parcelable superState) {
			super(superState);
		}

		public SavedState(Parcel source) {
			super(source);

			source.readStringList(entries);
			entryValues = (ArrayList<Integer>) source.readSerializable();
			selected = source.readInt();
		}

		@Override
		public void writeToParcel(Parcel dest, int flags) {
			super.writeToParcel(dest, flags);

			dest.writeArray(entries.toArray());
			dest.writeArray(entryValues.toArray());
			//dest.writeInt(selected);
		}
	}
}
